# -*- coding: utf-8 -*-
__author__ = 'aalexeev'

import sys
import fnmatch
import os
import logging
import HTMLParser

from docx import Document
from lxml import etree


logger = logging.getLogger('spam_application')

root = etree.Element('root')


def mainCycle():
    allFiles = []
    allFiles = retrieveAllFilesFromDir("C:\\Users\\aalexeev\\Desktop\\Deutsch SVN\\")
    for file in allFiles:
        readTheFile(file)
    serializeXML("output.xml")


def readTheFile(inputFileName):
    addFileHandler()
    if (inputFileName != ''):
        try:
            document = Document(inputFileName)
            table = document.tables[0]

            for row in table.rows:

                html_parser = HTMLParser.HTMLParser()
                d=row.cells[0].text
                r=row.cells[1].text
                if (d=='Deutsch' or r=='Russisch'): continue
                deu = html_parser.unescape(d)
                rus = html_parser.unescape(r)
                newexpression = etree.Element('expression')
                deutsch = etree.Element('deutsch')
                deutsch.text = deu
                russisch = etree.Element('russisch')
                russisch.text = rus
                newexpression.append(deutsch)
                newexpression.append(russisch)
                root.append(newexpression)

        except:
            logger.info("Input file occurs some problems in the file " + inputFileName)


def serializeXML(fileForSerialization):
    FILE = open(fileForSerialization, "w")
    FILE.writelines(etree.tostring(root,encoding="utf8", pretty_print=True,))
    FILE.close()
    et = etree.ElementTree(root)
    et.write(sys.stdout)


def addFileHandler():
    # create logger with 'spam_application'
    logger.setLevel(logging.DEBUG)
    # create file handler which logs even debug messages
    fh = logging.FileHandler('fault.log')
    fh.setLevel(logging.DEBUG)
    # create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.ERROR)
    # create formatter and add it to the handlers
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    # add the handlers to the logger
    logger.addHandler(fh)
    logger.addHandler(ch)


def retrieveAllFilesFromDir(ParentDir):
    matches = []
    for root, dirnames, filenames in os.walk(ParentDir):
        for filename in fnmatch.filter(filenames, "*.docx"):
            if ("Wortschatz" not in filename) and ("T-Systems" not in filename):
                continue
            else:
                matches.append(os.path.join(root, filename))
    return matches

# print retrieveAllFilesFromDir("C:/Users/aalexeev/Desktop/Deutsch SVN/")
mainCycle()







